#!/bin/sh
# ccase_share_symtab.sh
# 
# Description:
#   Create a derived object from a sniff symtab file
#
# Usage:
#   ccase_share_symtab.sh <project_dir> 
#                         <path_of_private_symtab> 
#                         <path_of_shared_dir>
#                         [<parse_options>]

DEBUG=1
prg=`basename $0`
prgdir=`dirname $0`

projectDir=$1
privatePath=$2
sharedDir=$3
parseOptions=$4
symtab=`basename $privatePath`
CRfile=$symtab".CR"
dirName=`basename $sharedDir`

test $DEBUG && echo  "========================================================="
test $DEBUG && echo  "Start $prg - `date`"
test $DEBUG && echo  "Project: $projectDir"

if [ ! -d $sharedDir ]; then
    test $DEBUG && echo  "Creating shared directory $sharedDir"
    cd `dirname $sharedDir`
    cleartool co -nc .
    cleartool mkdir -nc $dirName 
    cleartool ci -nc .
    cd $dirName
    cleartool mkelem -eltype text_file -nc $CRfile 
    cleartool ci -nc .
fi
cd $sharedDir

test $DEBUG && echo  "Creating derived object..."
$SHELL $prgdir/ccase_create_DO.sh $privatePath $sharedDir

if [ $? != 0 ]; then
    test $DEBUG && echo  "Creation of DO failed."
    test $DEBUG && echo  "End $prg - `date`"
    exit 1;
fi

cd $sharedDir

DOfile=`cleartool lsdo -me -short -stime | head -1`

if [ -z $DOfile ]; then
    test $DEBUG && echo  "No DO found."
    test $DEBUG && echo  "End $prg - `date`"
    exit 1;
fi

if [ ! -r $CRfile ]; then
    test $DEBUG && echo  "Creating element $CRfile"
    cleartool co -nc .
    cleartool mkelem -eltype text_file -nc $CRfile 
    cleartool ci -nc .
fi
if [ ! -w $CRfile ]; then
    cleartool co -nc $CRfile
fi

test $DEBUG && echo  "Creating configuration file $CRfile:"
cd $projectDir
( \
 echo  "DO="$DOfile; \
 echo  "--- options ---"; \
 echo  $parseOptions; \
 echo  "--- END options ---"; \
 echo  "--- configuration ---"; \
 cleartool ls -short -vob_only .; \
 echo  "--- END configuration ---"; \
) > $sharedDir/$CRfile

cd $sharedDir
cleartool ci -nc $CRfile

if [ $? = 0 ]; then
#    cleartool winkin $DOfile
    echo "Created shared symtab $symtab"
fi

test $DEBUG && echo  "End $prg - `date`"
